﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace SolutionFactory.DBAccess
{
    public class DBHelper
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static string connectionString = ConfigurationManager.ConnectionStrings[1].ConnectionString.ToString();

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="safeSql">sql语句</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string safeSql)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(safeSql, connection);
                return cmd.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 执行带参数的sql命令
        /// </summary>
        /// <param name="sql">sql命令</param>
        /// <param name="values">参数</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 执行带参数的sql命令
        /// </summary>
        /// <param name="sql">sql命令</param>
        /// <param name="commandType">命令类型</param>
        /// <param name="values">参数</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string sql, CommandType commandType, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = commandType;
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteNonQuery();
            }
        }

        /// <summary>
        /// 执行Scalar语句
        /// </summary>
        /// <param name="safeSql">sql语句</param>
        /// <returns>返回结果的第一行第一列</returns>
        public static object ExecuteScalar(string safeSql)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(safeSql, connection);
                return cmd.ExecuteScalar();
            }
        }
        /// <summary>
        /// 执行Scalar语句
        /// </summary>
        /// <param name="sql">sql</param>
        /// <param name="values">参数</param>
        /// <returns>返回结果的第一行第一列</returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteScalar();
            }
        }
        /// <summary>
        /// 执行Scalar语句
        /// </summary>
        /// <param name="sql">sql</param>
        /// <param name="commandType">命令类型</param>
        /// <param name="values">参数</param>
        /// <returns>返回结果的第一行第一列</returns>
        public static object ExecuteScalar(string sql, CommandType commandType, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = commandType;
                cmd.Parameters.AddRange(values);
                return cmd.ExecuteScalar();
            }
        }

        /// <summary>
        /// 执行Reader命令
        /// </summary>
        /// <param name="safeSql">sql语句</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(string safeSql)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand(safeSql, connection);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        /// <summary>
        /// 执行Reader命令
        /// </summary>
        /// <param name="safeSql">sql</param>
        /// <param name="values">参数</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] values)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        /// <summary>
        /// 执行Reader命令
        /// </summary>
        /// <param name="safeSql">sql</param>
        /// <param name="values">参数</param>
        /// <param name="commandType">命令类型</param>
        /// <returns>SqlDataReader</returns>
        public static SqlDataReader ExecuteReader(string sql, CommandType commandType, params SqlParameter[] values)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.CommandType = commandType;
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        /// <summary>
        /// 获取DataTable
        /// </summary>
        /// <param name="safeSql">sql语句</param>
        /// <returns>DataTable</returns>
        public static DataTable ReadDataTable(string safeSql)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataTable table = new DataTable();
                SqlCommand cmd = new SqlCommand(safeSql, connection);
                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                table.Load(reader);
                reader.Close();
                return table;
            }
        }
        /// <summary>
        /// 获取DataTable
        /// </summary>
        /// <param name="safeSql">sql</param>
        /// <param name="values">参数</param>
        /// <returns>DataTable</returns>
        public static DataTable ReadDataTable(string sql, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataTable table = new DataTable();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddRange(values);
                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                table.Load(reader);
                reader.Close();
                return table;
            }
        }
        /// <summary>
        /// 获取DataTable
        /// </summary>
        /// <param name="safeSql">sql</param>
        /// <param name="values">参数</param>
        /// <param name="commandType">命令类型</param>
        /// <returns>DataTable</returns>
        public static DataTable ReadDataTable(string sql, CommandType commandType, params SqlParameter[] values)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataTable table = new DataTable();
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.CommandType = commandType;
                cmd.Parameters.AddRange(values);
                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                table.Load(reader);
                reader.Close();
                return table;
            }
        }
    }
}
